package com.lmpop.trade.ts_area;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.trade.util.Cn2Spell;

public class Ts_areaInfo implements TradeInterf
{
  QueryList queryList;

  public Ts_areaInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ts_area ts_area = new Ts_area();
    ts_area.setArea_id(map.getString("area_id"));
    ts_area.setArea_name(map.getString("area_name"));
    ts_area.setUp_area_id(map.getString("up_area_id"));
    ts_area.setSort_no(map.getInt("sort_no"));

    String area_name = map.getString("area_name").trim().replaceAll("\\s", "");
    try {
      String en_name = Cn2Spell.converterToSpell(area_name);
      ts_area.setEn_name(en_name);
    } catch (Exception e) {
      System.out.println("==no translate==");
    }

    List arealist = getAreaClassByName(ts_area.getArea_name());
    if ((arealist == null) || (arealist.size() <= 0)) {
      String result_code = "0";
      try {
        insert(dbmMgr, ts_area);
      } catch (ApplicationException e) {
        e.printStackTrace();
        result_code = "1";
      }
      map.setString("result_code", result_code);
    }
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ts_area ts_area)
    throws ApplicationException
  {
    dbmMgr.update("insertTs_area", ts_area);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    String area_ids = "";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        StringBuffer parea = new StringBuffer();
        for (int i = 0; i < pkStr.length; i++) {
          if (!pkStr[i].equals("")) {
            parea.append(pkStr[i] + "|");
            parea.append(getAllAreaAttr(pkStr[i]));
          }
        }
        area_ids = parea.toString();
        String[] areaIds = parea.toString().split("\\|");
        for (String id : areaIds)
          delete(dbmMgr, id);
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("area_ids", area_ids);
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTs_area", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ts_area ts_area = new Ts_area();
    ts_area.setArea_id(map.getString("area_id"));
    ts_area.setArea_name(map.getString("area_name"));
    ts_area.setUp_area_id(map.getString("up_area_id"));
    ts_area.setSort_no(map.getInt("sort_no"));
    String area_name = map.getString("area_name").trim().replaceAll("\\s", "");
    try {
      String en_name = Cn2Spell.converterToSpell(area_name);
      ts_area.setEn_name(en_name);
    } catch (Exception e) {
      System.out.println("==no translate==");
    }

    String result_code = "0";
    try {
      update(dbmMgr, ts_area);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ts_area ts_area)
    throws ApplicationException
  {
    dbmMgr.update("updateTs_area", ts_area);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ts_area ts_area = (Ts_area)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ts_area.setStart(Integer.valueOf(start));
    ts_area.setLimit(Integer.valueOf(limit));
//    select *
//	<isParameterPresent>
//     <include refid="ts_areaPageCommon"/>
//  </isParameterPresent> 
//		order by sort_no desc
// 	<isNotNull prepend="LIMIT" property="start">
//	#start#,#limit#
//	</isNotNull> 	
//    from ts_area
//	<dynamic prepend="WHERE">
//		<isNotNull prepend="AND" property="area_name"> 
//    	 instr(area_name,#area_name#)>0
//	 	</isNotNull>
//		<isNotNull prepend="AND" property="up_area_id"> 
//    	  up_area_id = #up_area_id#
//	 	</isNotNull>	
//	</dynamic>
    return this.queryList.getList("selTs_areaList", ts_area);
  }

  public int getCountByObj(Object obj)
  {
    Ts_area ts_area = (Ts_area)obj;
    List list = this.queryList.getList("selTs_areaSum", ts_area);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTs_areaByPk", pk);
  }

  public boolean checkSonArea(String pk)
  {
    List areaList = this.queryList.getList("checkTs_areaSonByPk", pk);

    return (areaList != null) && (areaList.size() > 0);
  }

  public String getAreaNameById(String pk)
  {
    List areaList = this.queryList.getList("getTs_areaNameByPk", pk);
    String area_name = "";
    if ((areaList != null) && (areaList.size() > 0)) {
      Hashtable areaMap = (Hashtable)areaList.get(0);
      if (areaMap.get("area_name") != null) area_name = areaMap.get("area_name").toString();
    }
    return area_name;
  }
  public List getAreaInfoByLevel(String level) {
    List areaList = this.queryList.getList("getAreaInfoByLevel", level);
    return areaList;
  }

  public String getUpAreaId(String area_id)
    throws Exception
  {
    String up_area_id = "";
    List areaList = this.queryList.getList("getUpAreaId", area_id);
    if ((areaList != null) && (areaList.size() > 0)) {
      Hashtable map = (Hashtable)areaList.get(0);
      if (map.get("up_area_id") != null)
        up_area_id = map.get("up_area_id").toString();
    }
    return up_area_id;
  }

  public String getAllAreaAttr(String pk)
  {
    StringBuffer areaAttr = new StringBuffer();
    List sonlist = getSonAreaById(pk);
    if ((sonlist != null) && (sonlist.size() > 0))
    {
      for (int i = 0; i < sonlist.size(); i++)
      {
        Hashtable sonMap = (Hashtable)sonlist.get(i);
        String area_id = "";
        if (sonMap.get("area_id") != null)
        {
          area_id = sonMap.get("area_id").toString();
        }
        if (area_id.equals(""))
          continue;
        areaAttr.append(area_id + "|");
        if (!checkSonArea(area_id))
          continue;
        areaAttr.append(getAllAreaAttr(area_id));
      }

    }

    return areaAttr.toString();
  }

  public List getSonAreaById(String pk)
  {
    return this.queryList.getList("checkTs_areaSonByPk", pk);
  }

  public SysMap AreaSort(DbmInterf dbmMgr, SysMap map)
  {
    Ts_area ts_area = new Ts_area();
    String pkid_string = map.getString("sort");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] sort = pkStr[i].split(",");
          if (sort.length == 1)
          {
            ts_area.setArea_id(sort[0]);
            ts_area.setSort_no(0);
            updateSort(dbmMgr, ts_area);
          }
          if (sort.length != 2)
            continue;
          ts_area.setArea_id(sort[0]);
          ts_area.setSort_no(Integer.parseInt(sort[1]));
          updateSort(dbmMgr, ts_area);
        }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateSort(DbmInterf dbmMgr, Ts_area ts_area) throws ApplicationException {
    dbmMgr.update("updateTs_areaSort", ts_area);
  }

  public Map getAreaClassByUpId(String up_class)
  {
    Map ts_area = new Hashtable();
    ts_area.put("up_area_id", up_class);
    Map classMap = new Hashtable();
    String area_id = ""; String area_name = "";
    List list = this.queryList.getList("getAreaClassByUpId", ts_area);
    if ((list != null) && (list.size() > 0)) {
      Map map = new Hashtable();
      for (int i = 0; i < list.size(); i++) {
        map = (Hashtable)list.get(i);
        if (map.get("area_name") != null) {
          area_name = map.get("area_name").toString();
        }
        if (map.get("area_id") != null) {
          area_id = map.get("area_id").toString();
        }
        classMap.put(area_id, area_name);
      }
    }
    return classMap;
  }

  public Map getAreaClass()
  {
    Map ts_area = new Hashtable();
    Map classMap = new Hashtable();
    String area_id = ""; String area_name = "";
    List list = this.queryList.getList("getAreaClass", ts_area);
    if ((list != null) && (list.size() > 0)) {
      Map map = new Hashtable();
      for (int i = 0; i < list.size(); i++) {
        map = (Hashtable)list.get(i);
        if (map.get("area_name") != null) {
          area_name = map.get("area_name").toString();
        }
        if (map.get("area_id") != null) {
          area_id = map.get("area_id").toString();
        }
        classMap.put(area_id, area_name);
      }
    }
    return classMap;
  }

  public List getAreaClassByName(String areaname) {
    List arealist = this.queryList.getList("getAreaClassByname", areaname);
    return arealist;
  }
}